﻿using Common.AspNetCore;
using Log.Api.Extensions;
using Log.Api.IService;
using Log.Api.LogModels;
using Log.Api.QueryModel;
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Log.Api.Service
{
    public class SqlListenerLogService : CommonService
    {
        ////private IBaseEsContextService esContextService;
        private IEsClientProviderService clientProvider;
        private string indexName = "minigroupcaperrorlog";

        /// <summary>
        /// 索引名称
        /// </summary
        public SqlListenerLogService(IServiceProvider provider, IEsClientProviderService client) : base(provider)
        {
            this.clientProvider = client;
        }

        public ElasticClient GetClient()
        {
            return this.clientProvider.GetClient(indexName);
        }

        /// <summary>
        /// 插入
        /// </summary>
        public async Task<bool> InsertAsync<T>(T model) where T : class
        {
            var client = GetClient();
            var response = await client.CreateDocumentAsync(model);
            return response.IsValid;
        }

        /// <summary>
        /// 根据Id删除数据
        /// </summary>
        public bool DeleteById<T>(string id) where T : class
        {
            var client = GetClient();
            var response = client.Delete<T>(id);
            return response.IsValid;
        }

        /// <summary>
        /// 根据条件获取日志
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<SqlListenerLogModel> GetSqlListenerLoges(SqlListenerLogQuery query)
        {
            var client = GetClient();
            var response = client.Search<SqlListenerLogModel>(s => s.Query(q => (q.Match(w => w.Field(f => f.Time).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Sql).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Calls).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.AvgTime).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.TotalTime).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Rows).Query(query.Parameter)))
             && q.Match(w => w.Field(f => f.ApiKey).Query(query.ApiKey))
             && +q.DateRange(d => d.Field(f => f.Time).GreaterThanOrEquals(query.BeginTime).LessThan(query.EndTime))).Sort(s => s.Descending(d => d.Time)).Sort(s => s.Descending(d => d.AvgTime)).From((query.PageIndex - 1) * query.PageSize).Take(query.PageSize));
            return response.Documents.ToList();
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        public long GetTotalCount(SqlListenerLogQuery query)
        {
            var client = GetClient();
            var response = client.Search<SqlListenerLogModel>(s => s.Query(q => (q.Match(w => w.Field(f => f.Time).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Sql).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Calls).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.AvgTime).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.TotalTime).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Rows).Query(query.Parameter)))
             && q.Match(w => w.Field(f => f.ApiKey).Query(query.ApiKey))
             && +q.DateRange(d => d.Field(f => f.Time).GreaterThanOrEquals(query.BeginTime).LessThan(query.EndTime))));
            return response.Total;
        }

        /// <summary>
        /// 获取所有日志
        /// </summary>
        /// <returns></returns>
        public List<SqlListenerLogModel> GetAllSqlListenerLoges()
        {
            var client = GetClient();
            var searchDescriptor = new SearchDescriptor<SqlListenerLogModel>();
            searchDescriptor = searchDescriptor.Query(p => p.MatchAll());
            var response = client.Search<SqlListenerLogModel>(searchDescriptor);
            return response.Documents.ToList();
        }

        /// <summary>
        /// 删除指定日志数据
        /// </summary>
        /// <returns></returns>
        public bool DeleteByQuery(SqlListenerLogQuery query)
        {
            var client = GetClient();
            var response = client.Search<SqlListenerLogModel>(s => s.Query(q => (q.Match(w => w.Field(f => f.Time).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Sql).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Calls).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.AvgTime).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.TotalTime).Query(query.Parameter))
            || q.Match(w => w.Field(f => f.Rows).Query(query.Parameter)))
             && q.Match(w => w.Field(f => f.ApiKey).Query(query.ApiKey))
             && +q.DateRange(d => d.Field(f => f.CT).GreaterThanOrEquals(query.BeginTime).LessThan(query.EndTime))));
            return response.IsValid;
        }
    }
}
